/*
 * Package il.ac.biu.cs.grossmm.api.data
 * File BagNodeType.java
 * 
 * This is a part of presence service framework API. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package il.ac.biu.cs.grossmm.api.data;

import java.util.WeakHashMap;

/**
 * Represents a data node type with no properties and with a single node-set.
 * This node type is defined only by a type of nodes which may be contained in
 * the only node-set in this type. Two instances of this class are equal if they
 * are defined using the same child node type.
 * <p>
 * Example:
 * 
 * <pre>
 * // type of data nodes which contain a set of pidf nodes 
 * NodeType&lt;SetOf&lt;Pidf&gt;&gt; setOfPidfsType = setOf(Pidf.class);
 * 
 * // a reference to nodes of this type can be defined as follows
 * Node&lt;SetOf&lt;Pidf&gt;&gt; setOfPidfs;  
 * </pre>
 * 
 * 
 * @param <N>
 *            generic parameter of child node type (type of nodes which may be
 *            contained in the only node-set in this type)
 */
public class SetOf<N> implements NodeType<SetOf<N>> {
	/**
	 * Returns an instance of this class defined by a given node type of nodes
	 * 
	 * @param <N>
	 *            generic parameter of child node type
	 * @param nodeType
	 *            the child node type
	 * @return an instance of this class defined by a given node type
	 */
	@SuppressWarnings("unchecked")
	public static <N> SetOf<N> setOf(NodeType<N> nodeType)
	{
		SetOf<N> s = cache.get(nodeType);
		if (s == null) {
			s = new SetOf<N>(nodeType);
			cache.put(nodeType, s);
		}

		return s;
	}

	/**
	 * Returns an instance of this class defined by a given node type interface
	 * 
	 * @param <N>
	 *            generic parameter of child node type
	 * @param clazz
	 *            Class object of child node type interface
	 * @return an instance of this class defined by a given node type interface
	 */
	public static <N extends NodeInterface> SetOf<N> setOf(Class<N> clazz)
	{
		return new SetOf<N>(NodeTypeByInterface.nodeType(clazz));
	}

	/**
	 * The only property of corresponding node type
	 */
	public final Nodeset<SetOf<N>, N> NODESET;

	private NodeType<N> nested;

	static WeakHashMap<NodeType, SetOf> cache =
		new WeakHashMap<NodeType, SetOf>();

	static class NestedSet<N> implements Nodeset<SetOf<N>, N> {
		SetOf<N> parent;

		public NestedSet(SetOf<N> parent)
		{
			this.parent = parent;
		}

		public int index()
		{
			return 0;
		}

		public NodeType<N> nestedNodeType()
		{
			return parent.nested;
		}

		public NodeType<SetOf<N>> parentNodeType()
		{
			return parent;
		}

		@Override
		public String toString()
		{
			return "<"+parent.toString()+">";
		}
	}

	private SetOf(NodeType<N> nested)
	{
		this.NODESET = new NestedSet<N>(this);
		this.nested = nested;
	}

	public int nodesetsNumber()
	{
		return 1;
	}

	public int propertiesNumber()
	{
		return 0;
	}

	public Nodeset nodeset(int index)
	{
		if (index == 0)
			return NODESET;
		else
			throw new IndexOutOfBoundsException();
	}

	public Property property(int index)
	{
		throw new IndexOutOfBoundsException();
	}

	@Override
	public boolean equals(Object obj)
	{
		if (obj == null || !(obj instanceof SetOf))
			return false;

		SetOf s = (SetOf) obj;

		return nested.equals(s.nested);
	}

	@Override
	public int hashCode()
	{
		return nested.hashCode() ^ 0xac56410f;
	}

	@Override
	public String toString()
	{
		return "SetOf("+nested.toString()+")";
	}
}
